<?php
/* Copyright (c) 2012 Universidad Del Atlántico

  Autores
  Departamento de Admisiones y Registro Academico Universidad del Atlantico
  Carlos Fuentes

  /* All rights reserved.

  /* Redistribution and use in source and binary forms, with or without
  /* modification, are permitted provided that the following conditions
  /* are met:
  /* 1. Redistributions of source code must retain the above copyright
  /*    notice, this list of conditions and the following disclaimer.
  /* 2. Redistributions in binary form must reproduce the above copyright
  /*    notice, this list of conditions and the following disclaimer in the
  /*    documentation and/or other materials provided with the distribution.
  /* 3. Neither the name of copyright holders nor the names of its
  /*    contributors may be used to endorse or promote products derived
  /*    from this software without specific prior written permission.

  /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  /* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  /* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  /* PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
  /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  /* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  /* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  /* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  /* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  /* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  /* POSSIBILITY OF SUCH DAMAGE.


 */
session_start();
ob_start();

function __autoload($class_name) {
    $fullclasspath = "";
    $suffix = "";
    if ($class_name == "FirePHP") {
        $fullclasspath = "fire/";
        $suffix = ".class";
    }
    if (!file_exists('../inc/' . $fullclasspath . $class_name . ".php")) {

        if (!file_exists('../inc/' . $fullclasspath . $class_name . ".class.php")) {

            if (file_exists('../inc/' . $fullclasspath . $class_name . ".tpl.php")) {

                $suffix = ".tpl";
            } elseif (file_exists('../inc/utility/' . $class_name . ".class.php")) {

                $fullclasspath = "utility/";
                $suffix = ".class";
            } elseif (file_exists('../inc/db/' . $class_name . ".class.php")) {

                $fullclasspath = "db/";
                $suffix = ".class";
            }
        } else {

            $suffix = ".class";
        }
    }
//require '../inc/';
    require_once '../inc/' . $fullclasspath . $class_name . $suffix . '.php';
}

$firephp = FirePHP::getInstance(true);
$pl = new Template();
$user = new LoggedUser($_SESSION['usuario']);
if ($user->isAdmin()) {
    if (!empty($_POST)) {
        $db = new connection();
        $xm = new XmlDomConstruct();


        
        if (intval($_POST['nombre'])) {
            $contenido=  AlaniaContenidosModel::findById($db->db, intval($_POST['nombre']));
            $contenido->setPlantilla($xm->html2json($_POST['contentcreator']));
            $contenido->setAlaniaTipoInformacionIdTipoInformacion($_POST['tinfo']);
            $contenido->updateToDatabase($db->db);
        } else {
            $contenido = new AlaniaContenidosModel();
            $contenido->setNombre($_POST['nombre']);
            $contenido->setPlantilla($xm->html2json($_POST['contentcreator']));
            $contenido->setAlaniaTipoInformacionIdTipoInformacion($_POST['tinfo']);
            $contenido->insertIntoDatabase($db->db);
        }


        
        

        $contenido->updateInsertToDatabase($db->db);
    } else {

        $cont = $_GET['content'];
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <script src="js/jquery.js"></script>
            </script><script src='js/ckeditor/ckeditor.js' type='text/javascript'>
        </script><script type='text/javascript' src='js/ckeditor/adapters/jquery.js'>
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <script type='text/javascript'>
                $(document).ready(function(){
                    $('#contentcreator').ckeditor();
                });
            </script>
        </head>
        <body>
            <form id="formcrearcont" method="POST" name="formcrearcont">
                <table border="0" cellpadding="1" cellspacing="1" style="width: 100%">
                    <tbody>

                        <tr>

                            <?php
                            if (!$cont) {

                                echo "<td><label for = 'nombre'>Nombre de Contenido</label></td>";
                                echo "<td><input name = 'nombre' type = 'text' /></td>";
                            } else {

                                echo "<td><input name = 'nombre' type = 'hidden' value = '$cont' /></td>";
                            }
                            ?>
                        </tr>

                        <tr>

                            <td colspan="2"><textarea cols="80" name="contentcreator" id="contentcreator" rows="50">
                                    <?php
                                    if ($cont) {
                                        echo $pl->draw_for_edit($cont);
                                    }
                                    ?>
                                </textarea></td>
                        </tr>
                        <tr>

                            <tr><td colspan="2"><select id="tipoinfo" name="tinfo">
                                        <?php
                                        $db = new connection();
                                        $tinfo = AlaniaTipoInformacionModel::findAll($db->db);
                                        $firephp = FirePHP::getInstance(true);

                                        foreach ($tinfo as $key => $value) {

                                            echo ' <option value = "' . $value->getIdTipoInformacion() . '"';
                                            if ($cont && ($pl->getTInfo() == $value->getIdTipoInformacion())) {
                                                $firephp->log($pl);
                                                echo ' selected="selected"';
                                            }
                                            echo '>' . $value->getNombre() . '</option>';
                                        }
                                        ?>
                                    </select>
                            </tr>


                            <tr>
                                <td colspan="2"><input type="submit" value="Enviar"/></td>
                            </tr>
                    </tbody>
                </table>
            </form>
        </body>
        </html>
        <?php
    }
} else {
    echo "Usted No Tiene Autorizacion para este Módulo";
}
